package com.example.demo.service;

import com.example.demo.domain.receive.RewNewBookReceive;
import com.example.demo.domain.result.book.BookRewNewInfo;
import com.example.demo.exception.PasswordErrorException;
import com.example.demo.utils.HbutLoginUtil;
import com.example.demo.utils.ParseUtil;
import com.example.demo.utils.VerifyCodeUtil;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 续借图书
 *
 * @author lizijian
 */
@Slf4j
public class RenewBookService {
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0";
    private static final String LOGIN_URL = "https://sso.hbut.edu.cn:7002/cas/login?service=http://run.hbut.edu.cn/Account/sso";
    private static final String LIBRARY_CARD_URL = "https://sso.hbut.edu.cn:7002/cas/login?service=http%3A%2F%2F202.114.181.8%3A8080%2Freader%2Fhwthau.php%3F_tsp_%3D1617113170007";
    private static final String LIBRARY_BOOK_URL = "http://202.114.181.8:8080/reader/book_lst.php";

    /**
     * 解析数据,返回续借书籍所需信息
     */
    private List<BookRewNewInfo> excute(String username, String password) throws IOException, PasswordErrorException, ParseException {
        Map<String, String> cookie = HbutLoginUtil.loginSso(username, password);
        Connection.Response response = Jsoup.connect(LIBRARY_CARD_URL).cookies(cookie).timeout(10000).header("User-Agent", USER_AGENT).execute();
        Connection.Response response1 = Jsoup.connect(LIBRARY_BOOK_URL).cookies(response.cookies()).timeout(10000).header("User-Agent", USER_AGENT).execute();
        //解析图书馆借阅书籍部分
        Document libraryBook = response1.parse();
        System.out.println(libraryBook.body());
        //每一个tr代表一本书
        Elements tableLine = libraryBook.getElementsByClass("table_line");
        Elements tr = tableLine.select("tr");
        boolean isFirst = true;
        List<BookRewNewInfo> bookRewNewInfoList = new ArrayList<>(10);
        for (Element element : tr) {
            if (isFirst) {
                isFirst = false;
                continue;
            }
            BookRewNewInfo bookRewNewInfo = new BookRewNewInfo();
            bookRewNewInfo.setBookName(element.select("a").html());
            String endTime = element.select("font").html();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            long returnDate = simpleDateFormat.parse(endTime).getTime();
            long now = System.currentTimeMillis();
            //超期
            if (now > returnDate) {
                bookRewNewInfo.setOverdue(true);
            }
            //剩余还书时间大于三天
            else if ((returnDate - now) / (1000 * 60 * 60 * 24) > 3) {
                continue;
            }

            bookRewNewInfo.setEndDate(endTime);
            bookRewNewInfo.setRewNew("1".equals(element.getElementsByAttributeValue("width", "6%").html()));


            Elements input = element.select("input");
            String onclick = input.attr("onclick");
            String[] split = onclick.split("'");
            int sum = 0;
            for (String s : split) {
                if (sum == 1) {
                    bookRewNewInfo.setBarCode(s);
                } else if (sum == 3) {
                    bookRewNewInfo.setCheck(s);
                }
                sum++;
            }
            bookRewNewInfoList.add(bookRewNewInfo);

        }
        return bookRewNewInfoList;
    }



}
